﻿using MailBox.App_Code;
using MailBox.App_Code.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MailBox
{
    public partial class Details : System.Web.UI.Page
    {
        DataUtils utils = new DataUtils();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                userLogin = getUserLogin();
                if (userLogin != null)
                {
                    string mode = Request.QueryString["mode"];
                    string id = Request.QueryString["id"];
                    DataTable table = utils.GetInboxDTO(Session["EmailLogin"], id, mode);
                    if (table != null && table.Rows.Count == 1)
                    {
                        lblTitle.Text = table.Rows[0]["Subject"] + "";
                        lblTime.Text = Convert.ToDateTime(table.Rows[0]["createtime"] + "").ToString("dd/MM/yyyy - HH:mm");

                        lblEmail.Text = table.Rows[0]["sender"] + "";
                        lblReciever.Text = table.Rows[0]["InboxObject"] + "";

                        detail_sender_avatar.Src = !string.IsNullOrWhiteSpace(table.Rows[0]["avatar"] + "") ? table.Rows[0]["avatar"] + "" : "images/1386966026professional-man.png";

                        string avatar = getUserLogin().Account.Avatar;
                        if (string.IsNullOrWhiteSpace(avatar)) avatar = "images/1386966026professional-man.png";
                        detail_avatar.Src = avatar;

                        lblContetn.Text = table.Rows[0]["Content"] + "";
                        utils.UpdateOpen(id);
                        ((SiteMaster)Page.Master).CheckMail();
                        string path = table.Rows[0]["FileAttached"] + "";
                        //string path = "AttachFiles/" + Session["EmailLogin"] + "/" + table.Rows[0]["FileAttached"];
                        if (path.Trim() != "" && (Directory.Exists(Server.MapPath(table.Rows[0]["FileAttached"] + "")) || Directory.Exists(Server.MapPath("AttachFiles//" + Session["EmailLogin"] + "//" + table.Rows[0]["FileAttached"] + ""))))
                        //if (Directory.Exists("C:\\Users\\SONY\\Downloads"))
                        {
                            //string[] files = Directory.GetFiles("C:\\Users\\SONY\\Downloads");
                            if (mode == "Sent")
                            {
                                path = "AttachFiles//" + Session["EmailLogin"] + "//" + path;
                            }
                            string[] files = Directory.GetFiles(Server.MapPath(path));
                            DataTable T = new DataTable();
                            DataColumn col = new DataColumn("filename", typeof(string));
                            T.Columns.Add(col);
                            foreach (string file in files)
                            {
                                DataRow row = T.NewRow();
                                row[0] = Path.Combine(path, Path.GetFileName(file));
                                T.Rows.Add(row);
                            }
                            rptFiles.DataSource = T;
                            rptFiles.DataBind();
                        }
                    }
                }
            }
        }
        UserLoginDTO userLogin = null;
        private UserLoginDTO getUserLogin()
        {
            userLogin = (UserLoginDTO)Session["UserLogin"];
            if (userLogin == null)
                Response.Redirect("login.aspx", false);
            else return userLogin;
            return null;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void downloadfile(string file)
        {
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
            String Header = "Attachment; Filename=" + Path.GetFileName(file);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(file));
            HttpContext.Current.Response.WriteFile(Dfile.FullName);
            //Don't forget to add the following line
            HttpContext.Current.Response.End();
        }
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        public static void delete(decimal id, string mode)
        {
            DataUtils utils = new DataUtils();
            utils.DeleteEmail(id + "", mode);
        }
    }
}